﻿using Himp.TaskScheduling.Datas;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling
{
    /// <summary>
    /// 制样操作记录
    /// </summary>
    public class SamplingOperRecService : TaskSchedulingAppService
    {
        private readonly IRepository<SamplePreparationTask, string> _samplePreparationTaskRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgSendRecRepository"></param>
        /// <param name="hubContext"></param>
        public SamplingOperRecService(IRepository<SamplePreparationTask, string> samplePreparationTaskRepository)
        {
            _samplePreparationTaskRepository = samplePreparationTaskRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<SamplingOperRecDto>> GetListAsync(SamplingOperRecInput input)
        {
            var iQuery = await _samplePreparationTaskRepository.GetQueryableAsync();
            iQuery = iQuery.Where(i => i.TaskRecptTime.HasValue && i.TaskRecptTime.Value.Date == DateTime.Now.Date);
            iQuery = iQuery.OrderByDescending(i => i.TaskRecptTime);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<SamplePreparationTask>, List<SamplingOperRecDto>>(iQuery.ToList());

                return new PagedResultDto<SamplingOperRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<SamplePreparationTask>, List<SamplingOperRecDto>>(items.ToList());

                return new PagedResultDto<SamplingOperRecDto>(totalCount, itemsDto);
            }
        }
    }
}
